<?php

/*
 * Tomato Framework
 * 
 * Copyright 2012-2012 Luax.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @link http://www.tomatoframework.com
 * @link http://www.luaxlou.com
 * @version $Id: PostDao.class.php 63 2013-04-25 09:24:59Z luaxcn@gmail.com $
 */

/**
 * 
 * 
 * @package tomatoapp
 * @author Luax <luaxcn@gmail.com>
 */
class PostDao
{

    public static $pk = 'id';
    public static $modifyfields = 'catalog_id,title,content,description';
    public static $showfields = 'title,content,description,add_time,update_time';
    public static $listfields = 'content,description,add_time,update_time';
    public static $columns = array(
        'id' => 'int(11)',
        'catalog_id' => 'int(11)',
        'title' => 'varchar(20)',
        'description' => 'varchar(255)',
        'content' => 'text',
        'add_time' => 'datetime',
        'update_time' => 'datetime',
        'is_show' => 'tinyint(1)',
    );
    public static $autoCreates = array(
        'add_time' => 'now()',
        'update_time' => 'now()',
        'is_show' => '1',
    );
    public static $autoUpdates = array(
        'update_time' => 'now()',
    );

    public function getPostsByCatalogId($catalogId, $start = 0, $length = 10)
    {

        $keys = self::$listfields;
        $sql = "select 
                    id,
                    title,
                    {$keys}
                from blog_post
                where catalog_id = :catalog_id
                limit {$start},{$length}
            ";

        $sth = Load::db()->prepare($sql);
        $sth->execute(array(':catalog_id' => $catalogId));

        $arr = $sth->fetchAll(PDO::FETCH_ASSOC);

        return $arr;
    }

    public function findAll()
    {
        $keys = self::$listfields;
        $sql = "select 
                    id,
                    title,
                    {$keys}
                from blog_post
                limit 10000
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute();

        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }

    public function find($id)
    {
        $keys = self::$showfields;

        $sql = "select 
                    id,
                    {$keys} 
                from blog_post
                where id = ?
                limit 1
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute(array($id));

        return $sth->fetch(PDO::FETCH_ASSOC);
    }

    public function save($post)
    {


        $keys = self::$modifyfields;

        $values = implode(',', array_fill(0, sizeof(explode(',', $keys)), '?'));

        $data = array_values(SqlTemplate::filterFields($keys, $post));


        if (sizeof(self::$autoCreates) > 0)
        {
            $keys = $keys . ',' . implode(',', array_keys(self::$autoCreates));

            $values = $values . ',' . implode(',',
                            array_values(self::$autoCreates));
        }

        $sql = "insert into blog_post
                (
                    $keys
                )
                values
                (
                    $values
                )
            ";

        $db = Load::db();
        $sth = $db->prepare($sql);

        $result = $sth->execute($data);



        if ($result)
        {

            $lastInsertId = Load::db()->lastInsertId();

            return $lastInsertId;
        }

        return FALSE;
    }

    public function update($post)
    {

        $t = SqlTemplate::updatePairs(self::$modifyfields, ':');



        if (sizeof(self::$autoUpdates) > 0)
        {
            $arr = array();
            foreach (self::$autoUpdates as $k => $v)
            {

                $arr[] = "{$k}={$v}";
            }
            $t = $t . ',' . implode(',', $arr);
        }

        $sql = "update blog_post set
                $t
                where id = :id
                limit 1
            ";

        $sth = Load::db()->prepare($sql);

        return $sth->execute($post);
    }

    public function delete($id)
    {
        $sql = "delete 
                from blog_post
                where id = ?
                limit 1
            ";
        $sth = Load::db()->prepare($sql);

        return $sth->execute(array($id));
    }

}

?>